package com.controller;

import com.Interceptor.FailInterceptor;
import com.model.Member;
import com.services.MemberServices;
import com.utils.BaseController;
import com.utils.RedisUtil;
import com.utils.UUIDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.UUIDEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


/**
 * Created by yangziqiang on 16-9-12.
 */
@Controller
@RequestMapping("/member")
public class MemberController extends BaseController {


    @Autowired private MemberServices memberServices;

    @Autowired RedisUtil redisUtil;

    @FailInterceptor
    @RequestMapping("/Register")
    public String RegisterMember(){


        return  "/Register/Register";

    }

    @FailInterceptor
    @RequestMapping(value = "/addMember",method = RequestMethod.POST)
    public String addMember(String phone,String password){
        memberServices.addMember(phone, password);
        return  "login";
    }



    @FailInterceptor
    @RequestMapping(value = "/memberlogin",method = RequestMethod.POST)
    public String memberlogin(String mobile, String password,HttpServletRequest req){
        Member member = memberServices.queryByMember(mobile);
        if (member != null) {
            if (password.equals(member.getPassword())) {
                HttpSession session = req.getSession();
               // req.getSession().setAttribute("user",member);
                redisUtil.set(session.getId(),member);
                return  "redirect:/index/yzq";
            }else{
                return "redirect:/index/login";
            }
        }else{
            return "redirect:index/login";
        }
    }


    @RequestMapping("/loginout")
    public String loginout(HttpServletRequest request){
        String id = request.getSession().getId();
        redisUtil.remove(id);
        return  "redirect:/index/yzq";

    }


    @FailInterceptor
    @RequestMapping("/plinsert")
    @ResponseBody
    public int plinsert(){
        List<Member> members =new ArrayList<Member>();
        for (int i=0;i<6000;i++){
            Member member =new Member();
            member.setId(UUIDGenerator.generate());
            member.setNickname("yzq");
            member.setMobile("13128892235");
            member.setPassword("qwe123");
            members.add(member);
        }
        return memberServices.plinsert(members);
    }



}
